package com.srd.phone.activity;

import android.os.Bundle;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.shengrongdai.srd.R;
import com.srd.phone.adapter.TenderRecordAdapter;
import com.srd.phone.bean.TenderInfo;
import com.srd.phone.bean.TenderRecordInfo;
import com.srd.phone.mvp.presenters.BasePresenter;
import com.srd.phone.utils.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 投资记录
 */
public class TenderRecordDetailActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener {

    private PullToRefreshListView lv_list;

    private TenderRecordAdapter adapter;

    private String urlTrecordsList = Constants.BASEURL + "trecordsApp/trecordsList";  //投资记录
    private int page = 1;
    private List<TenderRecordInfo> list = new ArrayList<>();
    private TenderInfo info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        info = (TenderInfo) getIntent().getSerializableExtra("info");
        setContentView(R.layout.activity_tender_record_detail);
        initView();
        initData();
    }

    @Override
    public void initView() {
        tv_common_title.setText("投资记录");
        lv_list = (PullToRefreshListView) findViewById(R.id.lv_list);
        lv_list.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        lv_list.setOnRefreshListener(this);
        adapter = new TenderRecordAdapter(mContext);
        lv_list.setAdapter(adapter);
    }

    @Override
    public void initData() {
        presenter = new BasePresenter(requestQueue, this);
        setUrlArrs(urlTrecordsList);
        showWaitingDialog();
        getData();
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("mid", info.id + "");
        presenter.requestPostData(urlTrecordsList,map);

    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        cancelWaitingDialog();
        lv_list.onRefreshComplete();
        List<TenderRecordInfo> arrList = TenderRecordInfo.parse(data);
        list.addAll(arrList);
        adapter.setData(list);
        if (arrList.size() != 0) {
            page++;
        }
    }

    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
        getData();
    }
}
